﻿using DAL.DataProvider;
using DAL.DataTransferObject;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace DAL
{
    public class KhachHangDAO : DataAccess
    {
        public void KhachHang_AddNew(KhachHang kh)
        {
            SqlCommand cmd = new SqlCommand("INSERT INTO KhachHang VALUES(@MaKH,@SoCMND,@HoDemKH,@TenKH,@DiaChi,@SDT,@NgaySinh,@GioiTinh,@MST,@LoaiKH,@GhiChu)", DataAccess.GetConnection());
            cmd.Parameters.Add(new SqlParameter("@MaKH", SqlDbType.Char)).Value = kh.maKhachHang;
            cmd.Parameters.Add(new SqlParameter("@SoCMND", SqlDbType.Char)).Value = kh.CMND;
            cmd.Parameters.Add(new SqlParameter("@HoDemKH", SqlDbType.VarChar)).Value = kh.hoDemKH;
            cmd.Parameters.Add(new SqlParameter("@TenKH", SqlDbType.VarChar)).Value = kh.tenKH;
            cmd.Parameters.Add(new SqlParameter("@DiaChi", SqlDbType.VarChar)).Value = kh.diaChi;
            cmd.Parameters.Add(new SqlParameter("@SDT", SqlDbType.VarChar)).Value = kh.SDT;
            cmd.Parameters.Add(new SqlParameter("@NgaySinh", SqlDbType.Date)).Value = kh.ngaySinh.Date;
            cmd.Parameters.Add(new SqlParameter("@MST", SqlDbType.VarChar)).Value = kh.maSoThue;
            cmd.Parameters.Add(new SqlParameter("@LoaiKH", SqlDbType.VarChar)).Value = kh.loaiKH;
            cmd.Parameters.Add(new SqlParameter("@GhiChu", SqlDbType.VarChar)).Value = kh.ghiChu;
            cmd.Parameters.Add(new SqlParameter("@GioiTinh", SqlDbType.Char)).Value = kh.gioiTinh;
            cmd.ExecuteNonQuery();
        }
        public void KhachHang_Update(KhachHang kh)
        {
            SqlCommand cmd = new SqlCommand("UPDATE KhachHang SET DiaChi=@DiaChi,SDT=@SDT,LoaiKH=@LoaiKH,GhiChu=@GhiChu WHERE MaKH=@MaKH", DataAccess.GetConnection());
            cmd.Parameters.Add(new SqlParameter("@MaKH", SqlDbType.Char)).Value = kh.maKhachHang;
            cmd.Parameters.Add(new SqlParameter("@DiaChi", SqlDbType.VarChar)).Value = kh.diaChi;
            cmd.Parameters.Add(new SqlParameter("@SDT", SqlDbType.VarChar)).Value = kh.SDT;
            cmd.Parameters.Add(new SqlParameter("@LoaiKH", SqlDbType.VarChar)).Value = kh.loaiKH;
            cmd.Parameters.Add(new SqlParameter("@GhiChu", SqlDbType.VarChar)).Value = kh.ghiChu;
            cmd.ExecuteNonQuery();
        }
        public void KhachHang_Delete(KhachHang kh)
        {
            SqlCommand cmd = new SqlCommand("DELETE FROM KhachHang WHERE MaKH=@MaKH", DataAccess.GetConnection());
            cmd.Parameters.Add(new SqlParameter("@MaKH", SqlDbType.Char)).Value = kh.maKhachHang;
            cmd.ExecuteNonQuery();
        }
        public List<KhachHang> KhachHang_GetData()
        {
            List<KhachHang> listKH = new List<KhachHang>();
            SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM KhachHang", DataAccess.GetConnection());
            DataTable dt = new DataTable();
            sda.Fill(dt);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                KhachHang kh = new KhachHang();
                kh.maKhachHang = dt.Rows[i]["MaKH"].ToString();
                kh.CMND = dt.Rows[i]["SoCMND"].ToString();
                kh.hoDemKH = dt.Rows[i]["HoDemKH"].ToString();
                kh.tenKH = dt.Rows[i]["TenKH"].ToString();
                kh.diaChi = dt.Rows[i]["DiaChi"].ToString();
                kh.SDT = dt.Rows[i]["SDT"].ToString();
                if (dt.Rows[i]["NgaySinh"].ToString().Length == 0)
                {
                    kh.ngaySinh = DateTime.Now.Date;
                }
                else
                {
                    kh.ngaySinh = Convert.ToDateTime(dt.Rows[i]["NgaySinh"]);
                }
                kh.gioiTinh = dt.Rows[i]["GioiTinh"].ToString();
                kh.maSoThue = dt.Rows[i]["MST"].ToString();
                kh.loaiKH = dt.Rows[i]["LoaiKH"].ToString();
                kh.ghiChu = dt.Rows[i]["GhiChu"].ToString();
                listKH.Add(kh);
            }
            return listKH;
        }
        public List<KhachHang> KhachHang_SearchCustomer(string a)
        {
            List<KhachHang> listKH = new List<KhachHang>();
            SqlCommand cmd = new SqlCommand("SELECT * FROM KhachHang ", DataAccess.GetConnection());
            SqlDataReader dr;
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                if (a == dr["TenKH"].ToString())
                {
                    KhachHang kh = new KhachHang();
                    kh.maKhachHang = dr["MaKH"].ToString();
                    kh.hoDemKH = dr["HoDemKH"].ToString();
                    kh.tenKH = dr["TenKH"].ToString();
                    kh.gioiTinh = dr["GioiTinh"].ToString();
                    kh.diaChi = dr["DiaChi"].ToString();
                    kh.SDT = dr["SDT"].ToString();
                    kh.ngaySinh = Convert.ToDateTime(dr["NgaySinh"].ToString());
                    kh.maSoThue = dr["MST"].ToString();
                    kh.loaiKH = dr["LoaiKH"].ToString();
                    kh.ghiChu = dr["GhiChu"].ToString();
                    listKH.Add(kh);
                }
            }
            return listKH;
        }
    }
}
